//
//  DedailVC.m
//  News
//
//  Created by lanou3g on 15/11/5.
//  Copyright © 2015年 songhaowen. All rights reserved.
//

#import "DedailVC.h"
#import "SHXEntertainmentDetailDataHepler.h"
#import "JQnewsModel.h"
#import "SDCycleScrollView.h"
#define SW [UIScreen mainScreen].bounds.size.width  //屏宽
#define  SH [UIScreen mainScreen].bounds.size.height //屏高

@interface DedailVC ()
@property (nonatomic, copy) NSString *html;
@end

@implementation DedailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSLog(@"类---%@",self.model.docid);
  

    
    [self setUpNav];//导航栏
    [self  seUpUI];//详情界面展示内容
   
}

//---------------------导航栏
-(void)setUpNav
{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0,0,414,64)];
    btn.backgroundColor = ColorRandom;
    [btn setTitle:@"返回" forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(Action) forControlEvents:UIControlEventTouchUpInside];
}
-(void)Action
{
    [self back];
}
-(void)back
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
//---------------------------


//详情界面展示内容
-(void)seUpUI
{
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, 414, 672)];
    [self.view addSubview: webView];
    webView.backgroundColor = [UIColor grayColor];
    webView.tintColor = [UIColor grayColor];
  
 if(self.model.photosetID == nil)//图文版界面
 {
    
    [SHXEntertainmentDetailDataHepler sharedSHXEnDetail].block = ^(NSString *html) {
        self.html = html;
    };
  
    [[SHXEntertainmentDetailDataHepler sharedSHXEnDetail] getDataFromWebWithString:self.model];
    
    NSString *htmlstr = [[SHXEntertainmentDetailDataHepler sharedSHXEnDetail] getHtmlString];
    [webView loadHTMLString:htmlstr baseURL:nil];
    
 }
    
 else//轮播版界面
 {
     [self setUpNav];//导航栏
     
     //http://c.m.163.com/nc/article/AMJG50I100963VRR/full.html
     NSLog(@"%@",self.model.docid);
     
     
     NSString *str = [self.model.photosetID substringFromIndex:4];
     
     NSString *strr = [str substringFromIndex:5];
     
     NSString *strr1 = [str substringToIndex:4];
     
     NSString *photoStr = [NSString stringWithFormat:@"%@/%@",strr1,strr];
     NSString *url = [@"http://c.m.163.com/photo/api/set/" stringByAppendingString:photoStr];
     url = [url stringByAppendingString:@".json"];
     
     NSDictionary *dic = [self webDataWithStr:url];
     NSArray *array = [dic objectForKey:@"photos"];
     
     NSMutableArray *URLStringsArray = [NSMutableArray array];
     NSMutableArray *title = [NSMutableArray array];
     for (NSDictionary *dic in array)
     {
          [URLStringsArray addObject: dic[@"imgurl"]];
         [title addObject:dic[@"imgtitle"]];
         
     }
     
     NSArray *imagesURLStrings = [[NSArray array]arrayByAddingObjectsFromArray:URLStringsArray];
     NSArray *titlesArray = [[NSArray array] arrayByAddingObjectsFromArray:title];
     
      //网络加载 --- 创建带标题的图片轮播器
     SDCycleScrollView *cycleScrollView2 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 64, SW, SH-64) imageURLStringsGroup:nil]; // 模拟网络延时情景
     cycleScrollView2.autoScrollTimeInterval = 4;//4秒一换(不设置默认2秒)
     cycleScrollView2.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
     cycleScrollView2.titlesGroup = titlesArray;
     cycleScrollView2.dotColor = [UIColor grayColor]; // 自定义分页控件小圆标颜色
     cycleScrollView2.placeholderImage = [UIImage imageNamed:@"placeholder"];
//     cycleScrollView2.titleLabelHeight = 200;//
//     cycleScrollView2.titleLabelTextColor = [UIColor  ];//
     [self.view addSubview:cycleScrollView2];
     
     //             --- 模拟加载延迟
     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
         cycleScrollView2.imageURLStringsGroup = imagesURLStrings;
     });
 


 }
    
    
}

-(NSDictionary *)webDataWithStr:(NSString *)str
{
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    //    NSLog(@"%@",data);
    
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    
    return dict;
}


@end
